package com.dfwl.packageio.moudle;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dfwl.packageio.R;
import com.dfwl.packageio.core.base.BaseRVAdapter;
import com.dfwl.packageio.core.base.BaseViewHolder;
import com.dfwl.packageio.core.bean.BarCodeInfo;

import java.util.List;

/**
 * @auth &{zhujiule}
 * @date 2018/8/13
 * @copyright 杭州物恋网科技有限公司
 **/
public class PackageIoAdapter extends BaseRVAdapter<BarCodeInfo> {

   PackageIoListener mIoListener;

   int mCurrentFourcePosition = -1;


   /**
    * 构造器
    *
    * @param context 上下文
    * @param list    数据集合
    */
   public PackageIoAdapter(Context context, @NonNull List<BarCodeInfo> list) {
      super(context, list);
   }

   public int getmCurrentFourcePosition() {
      return mCurrentFourcePosition;
   }

   public void setmCurrentFourcePosition(int mCurrentFourcePosition) {
      this.mCurrentFourcePosition = mCurrentFourcePosition;
   }

   public void setPackageListener(PackageIoListener mIoListener){
      this.mIoListener = mIoListener;
   }
   @Override
   public int getLayoutId(int viewType) {
      return R.layout.item_package_layout;
   }

   @Override
   public void onBind(BaseViewHolder holder, final int position) {
      final BarCodeInfo mBarCodeInfo = getData(position);

      String expressCode =mBarCodeInfo.getExpressCode();
      TextView mTvPosition = holder.getTextView(R.id.tv_package_seq);
      mTvPosition.setText(position+1+"");

      final EditText mEditText = (EditText) holder.getTextView(R.id.et_package_num);
      if(!TextUtils.isEmpty(expressCode)){
         mEditText.setText(expressCode);
      }else{
         mEditText.setText("");
      }

      final Button mBtSave = holder.getButton(R.id.bt_package_save);

      if(mBarCodeInfo.isSaved()){
         mBtSave.setVisibility(View.VISIBLE);
      }else{
         mBtSave.setVisibility(View.INVISIBLE);
      }

      mBtSave.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            if(mIoListener!=null){
               String mBarcode = mEditText.getText().toString();
               if(!TextUtils.isEmpty(mBarcode)){
                  mIoListener.onClickSave(position,mBarcode);
               }
            }

         }
      });
      Button mBtDel = holder.getButton(R.id.bt_package_del);
      mBtDel.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            if(mIoListener!=null){
               mIoListener.onClickDel(position);
            }
         }
      });
      mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
         @Override
         public void onFocusChange(View view, boolean b) {
            if(b){
               setmCurrentFourcePosition(position);
            }
         }
      });
      mEditText.addTextChangedListener(new TextWatcher() {
         @Override
         public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

         }

         @Override
         public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

         }

         @Override
         public void afterTextChanged(Editable editable) {
            String mCurrentText = mEditText.getText().toString();
            String mOldText = mBarCodeInfo.getExpressCode();
            if(TextUtils.isEmpty(mOldText)&&!TextUtils.isEmpty(mCurrentText)){
               mBtSave.setVisibility(View.VISIBLE);
               mBarCodeInfo.setSaved(true);
            }else if(!TextUtils.isEmpty(mOldText)&&!TextUtils.isEmpty(mCurrentText)&&!mOldText.equals(mCurrentText)){
               mBtSave.setVisibility(View.VISIBLE);
               mBarCodeInfo.setSaved(true);
            }else{
               mBtSave.setVisibility(View.INVISIBLE);
               mBarCodeInfo.setSaved(false);
            }
         }
      });

   }

   public interface PackageIoListener{
      public void onClickSave(int position,String mBarCode);
      public void onClickDel(int position);
   }
}
